
clear all
set more off

use "MPPEC Dataset.dta"

gen id = _n
preserve

bysort pair: gen diff=priority2_match[2]-priority2_match[1] if _n==1

do hodges.do

restore

merge 1:1 id using treatment_comb



*** Fisher's randomization experiment

*Keele et al - subtract k from all treatment outcomes of interest for various k

*********************************************
*DIFFERENCE IN MEANS
*********************************************
foreach num of numlist -1(.005)0.5  {
preserve
replace priority2_match = priority2_match -`num'  if mobilization == 1
	sort pair mobilization
	capture drop diff
	bysort pair: gen diff=priority2_match[2]-priority2_match[1] if _n==1
	egen avg_diff_0=mean(diff) 
	drop diff

	
	
**************************************************************************
***                     Within pairs re-assignment of treatment
**************************************************************************

*preserve


local nsim=128                                                   // define the number of simulations
qui forvalues i=1/`nsim' {
	***    Running baseline analysis (difference bw treatment and control)
	**************************************************************************
	
	sort pair mobilization_`i'
	bysort pair: gen diff_`i'=priority2_match[2]-priority2_match[1] if _n==1
	egen avg_diff_`i'=mean(diff_`i')
	drop diff_`i' mobilization_`i'

}
***
qui {
	***    Manipulating the output from the simulations
	**************************************************************************
	
	drop if _n>1
	keep avg_diff_*
	gen id=_n-1
	
	reshape long avg_diff_, i(id) j(bsrep)
	ren avg_diff_ avg_diff_sim
	drop id
	gen avg_diff=avg_diff_sim[1]
	drop if bsrep==0
	
	sort avg_diff_sim
	local sign=sign(avg_diff[1])
	noi di `num'
	if `sign'>0 {
		
		qui count if avg_diff_sim>avg_diff
		local greater=r(N)
		
		
		*** P-value calculation for one-tailed hypothesis tests
				
		noi di "The p-value of the one-tailed test of the null hypothesis that the mean difference between Treatment and Control is 0 is:  " `=`greater'/_N'
		
}
	
	else {

		qui count if avg_diff_sim<avg_diff
		local smaller=r(N)
		
		
		
		*** P-value calculation for one-tailed hypothesis tests
			
		noi di "The p-value of the one-tailed test of the null hypothesis that the mean difference between Treatment and Control is 0 is:  " `=`smaller'/_N'
		
		}
	}

restore

}
************************************************
*MC NEMARS
************************************************

clear all
set more off

cd "C:\Users\Aletheia\Dropbox\Ryan\Kenya Local Gov Experiment\Data Archive"

use "Kenya Local Gov-August.dta"

gen id = _n

merge 1:1 id using treatment_comb
assert _merge == 3
drop _merge

sum priority2_match if mobilization == 1

gen mcnemar_0= r(sum)


local nsim=128                                                   // define the number of simulations
qui forvalues i=1/`nsim' {
	***    Running baseline analysis (difference bw treatment and control)
	**************************************************************************

	sum priority2_match if mobilization_`i' == 1

	gen mcnemar_`i'= r(sum)
	
	drop mobilization_`i'

}


qui {
	***    Manipulating the output from the simulations
	**************************************************************************
	
	drop if _n>1
	keep mcnemar_*
	gen id=_n-1
	
	reshape long mcnemar_, i(id) j(bsrep)
	ren mcnemar_ mcnemar_sim
	drop id
	gen mcnemar =mcnemar_sim[1]
	drop if bsrep==0
	
	sort mcnemar_sim

		
		qui count if mcnemar_sim< mcnemar
		local greater=r(N)
		
		
		*** P-value calculation for one-tailed hypothesis tests
				
		noi di "The p-value of the one-tailed test is:  " `=`greater'/_N'
		
}
	
